package com.chuangjiangx.member.coupon.ddd.query;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.member.basic.dao.mapper.InMbrConfigMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrConfig;
import com.chuangjiangx.member.basic.dao.model.InMbrConfigExample;
import com.chuangjiangx.member.basic.dao.model.InMbrWxCardExample;
import com.chuangjiangx.member.coupon.dao.mapper.InMbrCouponMapper;
import com.chuangjiangx.member.basic.dao.mapper.InMbrWxCardMapper;
import com.chuangjiangx.member.coupon.dao.model.InMbrCoupon;
import com.chuangjiangx.member.coupon.ddd.dal.mapper.MbrCardDalMapper;
import com.chuangjiangx.member.basic.ddd.query.dto.MbrConfigDTO;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrWxCardDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 会员卡查询
 *
 * @author wp
 * @date 2018-09-03 16:18
 **/
@Component
public class MbrCardQuery {

    private final InMbrConfigMapper inMbrConfigMapper;
    private final InMbrCouponMapper inMbrCouponMapper;
    private final InMbrWxCardMapper inMbrWxCardMapper;
    private final MbrCardDalMapper mbrCardDalMapper;

    @Autowired
    public MbrCardQuery(InMbrConfigMapper inMbrConfigMapper, InMbrCouponMapper inMbrCouponMapper, InMbrWxCardMapper inMbrWxCardMapper, MbrCardDalMapper mbrCardDalMapper) {
        this.inMbrConfigMapper = inMbrConfigMapper;
        this.inMbrCouponMapper = inMbrCouponMapper;
        this.inMbrWxCardMapper = inMbrWxCardMapper;
        this.mbrCardDalMapper = mbrCardDalMapper;
    }

    /**
     * 查询会员基础配置详情
     *
     * @param merchantId
     * @return
     */
    public MbrConfigDTO searchCardDetail(Long merchantId) {
        InMbrConfigExample example = new InMbrConfigExample();
        example.createCriteria().andMerchantIdEqualTo(merchantId);
        List<InMbrConfig> inMbrConfigList = inMbrConfigMapper.selectByExample(example);
        if (inMbrConfigList != null && inMbrConfigList.size() > 0) {
            MbrConfigDTO mbrConfigDTO = new MbrConfigDTO();
            InMbrConfig inMbrConfig = inMbrConfigList.get(0);
            BeanUtils.convertBean(inMbrConfig, mbrConfigDTO, (in, mb) -> {
                mb.setMbrConfigId(in.getId());
            });
            if (inMbrConfig.getGiftMbrCouponId() != null && inMbrConfig.getGiftMbrCouponId() != 0) {
                InMbrCoupon inMbrCoupon = inMbrCouponMapper.selectByPrimaryKey(inMbrConfig.getGiftMbrCouponId());
                mbrConfigDTO.setGiftMbrCouponName(inMbrCoupon == null ? null : inMbrCoupon.getName());
                mbrConfigDTO.setGiftCouponNumber(inMbrCoupon == null ? null : inMbrCoupon.getCouponNumber());
            }
            return mbrConfigDTO;
        }

        return null;
    }

    public MbrWxCardDTO searchWxCardDetail(Long merchantId) {
        return mbrCardDalMapper.searchWxCardDetail(merchantId);
    }

    public String searchWxCardId(Long merchantId){
        return mbrCardDalMapper.searchWxCardId(merchantId);
    }

    public boolean isMbrWxCard(String cardId){
        InMbrWxCardExample example = new InMbrWxCardExample();
        example.createCriteria().andWxCardIdEqualTo(cardId);
        Long count = inMbrWxCardMapper.countByExample(example);
        return count>0;
    }

}
